@extends('admin.base')


@section('content')
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            <i class="fa fa-calendar"></i>
            商品添加页面
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
            <li><a href="#">商品信息管理</a></li>
            <li class="active">添加商品信息</li>
        </ol>
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="row">
            <!-- right column -->
            <div class="col-md-12">
                <!-- Horizontal Form -->
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title"><i class="fa fa-plus"></i> 修改商品信息</h3>
                    </div><!-- /.box-header -->
                    <!-- form start -->
                    <form class="form-horizontal" action="{{URL('admin/goods/'.$data->id)}}" method="post" id="goods_form" enctype="multipart/form-data">
                        <input type="hidden" name="_method" value="put">
                        {{csrf_field()}}
                        <div class="box-body">
                            <div class="form-group">
                                <label for="inputPassword3" class="col-sm-2 control-label">父类名称</label>
                                <div class="col-sm-4">
                                    <select  name="cate_id" class="form-control" >
                                        @foreach($list as $v)
                                            @if($data->cate_id == $v->cate_id)
                                            <option selected value="{{$v->cate_id}}">{{$v->cate_name}}</option>
                                            @else
                                                <option value="{{$v->cate_id}}">{{$v->cate_name}}</option>
                                            @endif
                                        @endforeach
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="inputPassword3" class="col-sm-2 control-label" >商品名称</label>
                                <div class="col-sm-4">
                                    <input type="text" name="goods_title" value="{{$data->goods_title}}" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="inputPassword3" class="col-sm-2 control-label">商品图片</label>
                                <div class="col-sm-4">
                                    <input type="text" name="goods_img" id="goods_img" class="form-control" value="{{$data->goods_img}}">
                                    <input type="file" name="file_upload" id="file_upload" value="" class="form-control">
                                    <p><img src="{{url($data->goods_img)}}" alt="" id="img1" style="width:100px" ></p>
                                    <script type="text/javascript">
                                        $(function () {
                                            $("#file_upload").change(function () {
                                                $('img1').show();
                                                uploadImage();
                                            });
                                        });
                                        function uploadImage() {
//                            判断是否有选择上传文件
                                            var imgPath = $("#file_upload").val();
                                            if (imgPath == "") {
                                                alert("请选择上传图片！");
                                                return;
                                            }
                                            //判断上传文件的后缀名
                                            var strExtension = imgPath.substr(imgPath.lastIndexOf('.') + 1);
                                            if (strExtension != 'jpg' && strExtension != 'gif'
                                                && strExtension != 'png' && strExtension != 'bmp') {
                                                alert("请选择图片文件");
                                                return;
                                            }
                                            var formData = new FormData($('#goods_form')[0]);
                                            $.ajax({
                                                type: "POST",
                                                url: "/admin/upload",
                                                data: formData,
                                                async: true,
                                                cache: false,
                                                contentType: false,
                                                processData: false,
                                                success: function(data) {
                                                    $('#img1').attr('src','/'+data);
                                                    $('#img1').show();
                                                    $('#goods_img').val(data);


                                                },
                                                error: function(XMLHttpRequest, textStatus, errorThrown) {
                                                    alert("上传失败，请检查网络后重试");
                                                }
                                            });
                                        }

                                    </script>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="inputPassword3" class="col-sm-2 control-label">商品价格</label>
                                <div class="col-sm-4">
                                    <input type="text" name="goods_price" class="form-control" value="{{$data->goods_price}}">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="inputPassword3" class="col-sm-2 control-label">商品信息</label>
                                <div class="col-sm-4">
                                    <input type="text" name="goods_info" class="form-control" value="{{$data->goods_info}}">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="inputPassword3" class="col-sm-2 control-label">商品状态</label>
                                <div class="col-sm-4">
                                    <select  name="goods_status" class="form-control" value="{{old('goods_status')}}">
                                        <option  value="1"@if($data->goods_status=1)"selected"@endif>新添加</option>
                                        <option  value="2"@if($data->goods_status=2)"selected"@endif>在售</option>
                                        <option  value="3"@if($data->goods_status=3)"selected"@endif>已下架</option>
                                    </select>
                                </div>
                            </div>
                        </div><!-- /.box-body -->
                        <div class="box-footer">
                            <div class="col-sm-offset-2 col-sm-1">
                                <button type="submit" class="btn btn-primary">修改</button>
                            </div>
                            <div class="col-sm-1">
                                <button type="submit" class="btn btn-primary">重置</button>
                            </div>
                        </div><!-- /.box-footer -->
                    </form>
                    <div class="row"><div class="col-sm-12">&nbsp;</div></div>
                    <div class="row"><div class="col-sm-12">
                            <br/>
                            @if (count($errors) > 0)
                                <div class="alert alert-danger">
                                    <ul>
                                        @foreach ($errors->all() as $error)
                                            <li>{{ $error }}</li>
                                        @endforeach
                                    </ul>
                                </div>
                            @endif
                        </div></div>
                </div><!-- /.box -->

            </div><!--/.col (right) -->
        </div>   <!-- /.row -->
    </section><!-- /.content -->

@endsection